//index.js
//获取应用实例
const app = getApp()
const qiniuUploader = require("../../utils/qiniuUploader");
Page({
  data: {
    src: null,
    userType: null,
    userId: null,
    base64Src: null,
    familyId: null,
  },
  onLoad: function(options) {
    this.setData({
      src: options.src,
      userType: options.userType,
      userId: options.userId,
      familyId: options.familyId,
    })
  },
  postFace() {
    var that = this;
    wx.request({
      url: app.globalData.serverFaceUrl,
      data: {
        'userId': that.data.userId,
        'userType': that.data.userType,
        'photoStr': that.data.base64Src,
        'familyId': that.data.familyId,
        'cmd': 'checkphoto',
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.redirectTo({
            url: '../result/result?result=' + 0 + "&userType=" + that.data.userType + "&userId=" + that.data.userId + "&familyId=" + that.data.familyId,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000,
            mask: true
          })
          console.log(res.data.message);
          wx.redirectTo({
            url: '../result/result?result=' + 1 + "&userType=" + that.data.userType + "&userId=" + that.data.userId + "&familyId=" + that.data.familyId,
          })
        }

      },
      fail: function(res) {
        wx.showToast({
          title: "上传失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        wx.redirectTo({
          url: '../result/result?result=' + 1 + "&userType=" + that.data.userType + "&userId=" + that.data.userId + "&familyId=" + that.data.familyId,
        })
        
      },
      complete: function(res) {
        console.log('submit complete');
      }
    })
  },

  btnFace() {
    var path = this.data.src;
    var that = this;
    wx.getFileSystemManager().readFile({
      filePath: path, //选择图片返回的相对路径
      encoding: 'base64', //编码格式
      success: res => {
        console.log('data:image/png;base64,' + res.data) //成功的回调
        that.setData({
          base64Src: res.data
        })
        that.postFace()
      }
    })
  },
})